#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
from easygconf.gconfdict import GConfDict
import time
import random
import os
from pysqlite2 import dbapi2 as sqlite
import gtk
import pygtk

from functions import *
 
dirname = os.path.join(os.path.expanduser("~"), "pics/wallpaper/") #change this to directory containing wallpapers, assuming it's in your home directory. If it's not, remove the os.path shit and give the full path name, starting from root (/).
rotate_mode = "ordered"

def quit_cb(widget, data = None):
    if data:
        data.set_visible(False)
    gtk.main_quit()

def popup_menu_cb(widget, button, time, data = None):
    if button == 3:
        if data:
            data.show_all()
            data.popup(None, None, None, 3, time)
    pass

def activate_icon_cb(widget, data = None):
    randomize(wallpapers,dirname,gc,rotate_mode)

def toggle_rotate(widget, data):
    global rotate_mode
    if data == "ordered":
        rotate_mode = "ordered"
    if data == "random":
        rotate_mode = "random"

def change_display_type(widget, data):
    x = find_wallpaper(wallpapers,dirname,gc)
    del wallpapers[x-1]
    (path, filename) = os.path.split(gc["picture_filename"])
    wallpapers.append((x,filename,data))
    gc["picture_options"] = data
    gc.sync()
    connection = sqlite.connect("wallpaper_db")
    cursor = connection.cursor()
    cursor.execute('UPDATE wallpapers SET display=? WHERE id=?', (data,x))
    connection.commit()
    cursor.close()
    connection.close()


wallpapers = load_wallpapers()

gc = GConfDict('/desktop/gnome/background')




statusIcon = gtk.StatusIcon()

rotate_toggle = gtk.Menu()
menuItem = gtk.MenuItem("Ordered")
menuItem.connect('activate', toggle_rotate, "ordered")
rotate_toggle.append(menuItem)
menuItem = gtk.MenuItem("Random")
menuItem.connect('activate', toggle_rotate, "random")
rotate_toggle.append(menuItem)

menu = gtk.Menu()
menuItem = gtk.ImageMenuItem(gtk.STOCK_QUIT)
menuItem.connect('activate', quit_cb)
menu.append(menuItem)

menuItem = gtk.MenuItem("toggle rotate mode")
menuItem.set_submenu(rotate_toggle)
menu.append(menuItem)

menuItem = gtk.MenuItem()
menu.append(menuItem)
menuItem = gtk.MenuItem("Tiled")
menuItem.connect('activate', change_display_type, "tiled")
menu.append(menuItem)
menuItem = gtk.MenuItem("Zoomed")
menuItem.connect('activate', change_display_type, "zoom")
menu.append(menuItem)
menuItem = gtk.MenuItem("Centered")
menuItem.connect('activate', change_display_type, "centered")
menu.append(menuItem)
menuItem = gtk.MenuItem("Scaled")
menuItem.connect('activate', change_display_type, "scaled")
menu.append(menuItem)
menuItem = gtk.MenuItem("Stretched")
menuItem.connect('activate', change_display_type, "stretched")
menu.append(menuItem)

statusIcon.set_from_file("icon.png")
statusIcon.set_tooltip("Critter's Awesome Wallpaper Randomizer!")
statusIcon.connect('activate', activate_icon_cb)
statusIcon.connect('popup-menu', popup_menu_cb, menu)
statusIcon.set_visible(True)


gtk.main()

    